package com.after.saas.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.after.saas.common.constants.ErrorCodeEnum;
import com.after.saas.common.constants.JwtEnum;
import com.after.saas.common.constants.StatusEnum;
import com.after.saas.common.exception.BadUserException;
import com.after.saas.common.utils.JwtUtil;
import com.after.saas.common.utils.LoginUtil;
import com.after.saas.common.utils.PasswordUtil;
import com.after.saas.common.api.service.ExternUserService;
import com.after.saas.common.model.query.UserQuery;
import com.after.saas.common.model.vo.UserVO;
import com.after.saas.user.mapper.UserMapper;
import com.after.saas.user.model.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.Collections;

/**
 * @Classname ExternUserServiceImpl
 * @Description TODO
 * @Date 2021/4/25 17:20
 * @Created by hjchen
 */

@Slf4j
@DubboService
public class ExternUserServiceImpl implements ExternUserService {

    @Value("${jwt.encode.secret}")
    private String secret;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public UserVO login(UserQuery query) {
        log.warn("{} login starting...", query.getUsername());
        UserVO dbUser = userMapper.findOne(query);
        if (ObjectUtil.isEmpty(dbUser))
            throw new BadUserException(ErrorCodeEnum.USER_NOT_EXIST);

        if (StatusEnum.DISABLE.value() == dbUser.getStatus())
            throw new BadUserException(ErrorCodeEnum.USER_DISABLED);

        if (!PasswordUtil.verify(query.getPassword(), new PasswordUtil.HashPassword(dbUser.getPassword(), dbUser.getSalt())))
            throw new BadUserException(ErrorCodeEnum.USER_PASSWD_BAD);

        dbUser.setToken(JwtUtil.generateToken(dbUser.getId().toString(), secret, JwtEnum.WEB_FRONT));
        dbUser.setRoles(Collections.singletonList("admin"));
        dbUser.setPermissions(Arrays.asList("user:*", "user:del"));
        LoginUtil.refreshUserInfo(redisTemplate, dbUser);
        return dbUser;
    }

    @Override
    public PageInfo<UserVO> listPage(UserQuery query) {
        PageHelper.startPage(query.getPage(), query.getSize());
        return new PageInfo<>(userMapper.listPage(query));
    }

    public UserVO registry(UserVO userVO) {
        User user = new User();
        BeanUtils.copyProperties(userVO, user);
        PasswordUtil.HashPassword hashPassword = PasswordUtil.generate(user.getPassword());
        user.setSalt(hashPassword.salt);
        user.setPassword(hashPassword.password);
        userMapper.insert(user);
        userVO.setId(user.getId());
        return userVO;
    }
}